package config_manager

import (
	"go_server/game_server/constants"
	"go_server/game_server/dataconfig/configpb"
	"go_server/zinx/zlog"
)

var (
	buildConfigByBlockId map[int32][]configpb.StcTable_Map3DConfig_Sheet_Build_7E655245
	blockConfigByMapId   map[int32][]configpb.StcTable_Map3DConfig_Sheet_Block_A734F594
	buildConfigWall      []int32

	systemUnlockMapByBuildLv  map[int32]map[int32]int32 //通过建筑等级解锁的功能集合，值={建筑ID，{功能ID，条件数值}}
	systemUnlockMapByHeroStar map[int32][]int32         //通过英雄星级解锁的功能集合，值={英雄星级,[]功能ID}
)

// ##################对外提供的接口###############################
func GetTableMap3DConfigSheetBlockConfig(id int32) configpb.StcTable_Map3DConfig_Sheet_Block_A734F594 {
	config, ok := tableMap3DConfigSheetBlock[id]
	if !ok {
		zlog.Errorf("GetTableMap3DConfigSheetBlockConfig Err!!! id: %d", id)
		return configpb.StcTable_Map3DConfig_Sheet_Block_A734F594{}
	}
	return configpb.StcTable_Map3DConfig_Sheet_Block_A734F594{
		Id:                 config.Id,
		MapId:              config.MapId,
		RelativeMapIdx:     config.RelativeMapIdx,
		FogNum:             config.FogNum,
		NextBlockId:        config.NextBlockId,
		UnlockDestroyWalls: config.UnlockDestroyWalls,
		AssistWalls:        config.AssistWalls,
	}
}

func GetTableMap3DConfigSheetBuildConfig(id int32) configpb.StcTable_Map3DConfig_Sheet_Build_7E655245 {
	config, ok := tableMap3DConfigSheetBuild[id]
	if !ok {
		zlog.Errorf("GetTableMap3DConfigSheetBlockConfig Err!!! id: %d", id)
		return configpb.StcTable_Map3DConfig_Sheet_Build_7E655245{}
	}
	return configpb.StcTable_Map3DConfig_Sheet_Build_7E655245{
		Id:                   config.Id,
		BlockId:              config.BlockId,
		BuildId:              config.BuildId,
		BuildLv:              config.BuildLv,
		RelativeBlockIdx:     config.RelativeBlockIdx,
		RelativeBlockForward: config.RelativeBlockForward,
		UnlockFogs:           config.UnlockFogs,
		ClickType:            config.ClickType,
	}
}

func GetTableMap3DConfigSheetBuildConfigBySceneId(sceneId int32) *configpb.StcTable_Map3DConfig_Sheet_Build_7E655245 {
	var config *configpb.StcTable_Map3DConfig_Sheet_Build_7E655245

	if tableMap3DConfigSheetBuild != nil {
		for _, val := range tableMap3DConfigSheetBuild {
			if sceneId == val.LevelChapterID { //根据 梦境关卡id查找 建筑id
				row := configpb.StcTable_Map3DConfig_Sheet_Build_7E655245{
					Id:                   val.Id,
					BlockId:              val.BlockId,
					BuildId:              val.BuildId,
					BuildLv:              val.BuildLv,
					RelativeBlockIdx:     val.RelativeBlockIdx,
					RelativeBlockForward: val.RelativeBlockForward,
					UnlockFogs:           val.UnlockFogs,
					ClickType:            val.ClickType,
					LevelChapterID:       val.LevelChapterID,
				}
				config = &row
			}
		}
	}
	return config
}

func GettableMap3DConfigSheetSundriesById(id int32) configpb.StcTable_Map3DConfig_Sheet_Sundries_838387F6 {
	config, ok := tableMap3DConfigSheetSundries[id]
	if !ok {
		zlog.Errorf("GettableMap3DConfigSheetSundriesById Err!!! id: %d", id)
		return configpb.StcTable_Map3DConfig_Sheet_Sundries_838387F6{}
	}

	return configpb.StcTable_Map3DConfig_Sheet_Sundries_838387F6{
		Id:         config.Id,
		Reward:     config.Reward,
		Condition:  config.Condition,
		UnlockFogs: config.UnlockFogs,
		ClickType:  config.ClickType,
	}
}

func GetTableMap3DConfigSheetMapMap() map[int32]*configpb.StcTable_Map3DConfig_Sheet_Map_252E6EDF {
	return tableMap3DConfigSheetMap
}

func GetTableMap3DConfigSheetBuildMap() map[int32]*configpb.StcTable_Map3DConfig_Sheet_Build_7E655245 {
	return tableMap3DConfigSheetBuild
}

func GetTableMap3DConfigSheetBlockMap() map[int32]*configpb.StcTable_Map3DConfig_Sheet_Block_A734F594 {
	return tableMap3DConfigSheetBlock
}

func GetTableMap3DConfigSheetWallMap() map[int32]*configpb.StcTable_Map3DConfig_Sheet_Wall_9A8E0E29 {
	return tableMap3DConfigSheetWall
}

func GetTableMap3DConfigSheetMapConfig(id int32) configpb.StcTable_Map3DConfig_Sheet_Map_252E6EDF {
	config, ok := tableMap3DConfigSheetMap[id]
	if !ok {
		zlog.Errorf("GetTableMap3DConfigSheetBlockConfig Err!!! id: %d", id)
		return configpb.StcTable_Map3DConfig_Sheet_Map_252E6EDF{}
	}
	return configpb.StcTable_Map3DConfig_Sheet_Map_252E6EDF{
		Id:               config.Id,
		GridUnitSize:     config.GridUnitSize,
		BlockNum:         config.BlockNum,
		BlockUnitGridNum: config.BlockUnitGridNum,
		BlockSpacing:     config.BlockSpacing,
	}
}

func GetTableMap3DConfigSheetWallConfig(id int32) configpb.StcTable_Map3DConfig_Sheet_Wall_9A8E0E29 {
	config, ok := tableMap3DConfigSheetWall[id]
	if !ok {
		zlog.Errorf("GetTableMap3DConfigSheetBlockConfig Err!!! id: %d", id)
		return configpb.StcTable_Map3DConfig_Sheet_Wall_9A8E0E29{}
	}
	return configpb.StcTable_Map3DConfig_Sheet_Wall_9A8E0E29{
		Id:                   config.Id,
		MapId:                config.MapId,
		GridNum:              config.GridNum,
		RelativeBlockIdx:     config.RelativeBlockIdx,
		RelativeBlockForward: config.RelativeBlockForward,
		RepairNeedCondition:  config.RepairNeedCondition,
		RepairNeedCost:       config.RepairNeedCost,
	}
}

func GetTableButtonSheetButtonConfig(id int32) configpb.StcTableButton_SheetButton_513D7B2C {
	config, ok := tableButtonSheetButton[id]
	if !ok {
		zlog.Errorf("GetTableButtonSheetButtonConfig Err!!! id: %d", id)
		return configpb.StcTableButton_SheetButton_513D7B2C{}
	}
	return configpb.StcTableButton_SheetButton_513D7B2C{
		Id:    config.Id,
		ZName: config.ZName,
		Ico:   config.Ico,
	}
}

func GetTableBuildSheetLevelConfig(id int32, level int32) configpb.StcTableBuild_SheetLevel_295B238C {
	config, ok := tableBuildSheetLevel[id][level]
	if !ok {
		zlog.Errorf("GetTableBuildSheetLevelConfig Err!!! id: %d", id)
		return configpb.StcTableBuild_SheetLevel_295B238C{}
	}
	return configpb.StcTableBuild_SheetLevel_295B238C{
		Id:         config.Id,
		Type:       config.Type,
		Name:       config.Name,
		Level:      config.Level,
		Condition1: config.Condition1,
		Condition2: config.Condition2,
		Consume:    config.Consume,
		Effect:     config.Effect,
		Power:      config.Power,
		Prosperity: config.Prosperity,
		SpendTime:  config.SpendTime,
	}
}

func GetTableBuildSheetBuildConfig(id int32) (configpb.StcTableBuild_SheetBuild_3ADA1586, bool) {
	config, ok := tableBuildSheetBuild[id]
	if !ok {
		zlog.Errorf("GetTableBuildSheetBuildConfig Err!!! id: %d", id)
		return configpb.StcTableBuild_SheetBuild_3ADA1586{}, ok
	}

	return configpb.StcTableBuild_SheetBuild_3ADA1586{
		Id:      config.Id,
		Type:    config.Type,
		ZName:   config.ZName,
		ModelId: config.ModelId,
		GridNum: config.GridNum,
		Button:  config.Button,
	}, true
}

func GetTableBuildSheetBuildConfigIds(typeid int32) map[int32]bool {
	rst := make(map[int32]bool, 0)
	for k, v := range tableBuildSheetBuild {
		if v.Type == typeid {
			rst[k] = true
		}
	}
	return rst
}

func Init() {
	setBuildConfigGroupByBlockId()
	setBlockConfigGroupByMapId()
	setBuildConfigWallGroup()
	setSystemUnlockType()
}

func setSystemUnlockType() {
	systemUnlockMapByBuildLv = make(map[int32]map[int32]int32)
	systemUnlockMapByHeroStar = make(map[int32][]int32)
	for id, info := range tableUnlockSheetUnlock {
		unlocks := info.Unlocks
		if unlocks[0] == constants.UNLOCK_TYPE_BUILD_LEVEL {
			if _, ok := systemUnlockMapByBuildLv[unlocks[1]]; !ok {
				systemUnlockMapByBuildLv[unlocks[1]] = map[int32]int32{id: unlocks[2]}
			} else {
				systemUnlockMapByBuildLv[unlocks[1]][id] = unlocks[2]
			}
		} else if unlocks[0] == constants.UNLOCK_TYPE_HERO_STAR {
			systemUnlockMapByHeroStar[unlocks[1]] = append(systemUnlockMapByHeroStar[unlocks[1]], id)
		}
	}
}

func setBlockConfigGroupByMapId() {
	blockMap := GetTableMap3DConfigSheetBlockMap()
	blockConfigByMapId = make(map[int32][]configpb.StcTable_Map3DConfig_Sheet_Block_A734F594)
	for _, config := range blockMap {
		mapId := config.MapId
		_, ok := blockConfigByMapId[mapId]
		if !ok {
			blockConfigByMapId[mapId] = make([]configpb.StcTable_Map3DConfig_Sheet_Block_A734F594, 0)
		}
		blockConfigByMapId[mapId] = append(blockConfigByMapId[mapId], *config)
	}
}

func setBuildConfigWallGroup() {
	buildConfigWall = make([]int32, 0, 1)
	for _, config := range tableBuildSheetBuild {
		typeId := config.Type
		if typeId != constants.Build_Info_EnclosingWall {
			continue
		}
		buildConfigWall = append(buildConfigWall, config.Id)
	}
}

func setBuildConfigGroupByBlockId() {
	buildMap := GetTableMap3DConfigSheetBuildMap()
	buildConfigByBlockId = make(map[int32][]configpb.StcTable_Map3DConfig_Sheet_Build_7E655245)

	for _, config := range buildMap {
		blockId := config.BlockId
		_, ok := buildConfigByBlockId[blockId]
		if !ok {
			buildConfigByBlockId[blockId] = make([]configpb.StcTable_Map3DConfig_Sheet_Build_7E655245, 0)
		}
		buildConfigByBlockId[blockId] = append(buildConfigByBlockId[blockId], *config)
	}
}

func GetBuildConfigByBlockID(blockId int32) []configpb.StcTable_Map3DConfig_Sheet_Build_7E655245 {
	return buildConfigByBlockId[blockId]
}

func GetBlockCountByMapID(mapId int32) int32 {
	return int32(len(blockConfigByMapId[mapId]))
}

func GetBlockArrByMapId(mapId int32) []configpb.StcTable_Map3DConfig_Sheet_Block_A734F594 {
	return blockConfigByMapId[mapId]
}

func GetWallConfigIds() []int32 {
	return buildConfigWall
}

func GetTableRobotSheetRobot(id int32) *configpb.StcTableRobot_SheetRobot_E97F9FC6 {
	config, ok := tableRobotSheetRobot[id]
	if !ok {
		zlog.Errorf("GetTableRobotSheetRobot Err!!! id: %d", id)
		return &configpb.StcTableRobot_SheetRobot_E97F9FC6{}
	}
	return config
}

func GetTableGameSettingSheetGame() configpb.StcTable_GameSetting_Sheet_Game_A7D9BDC0 {
	return configpb.StcTable_GameSetting_Sheet_Game_A7D9BDC0{
		RobotBuildTypeId:      tableGameSettingSheetGame.RobotBuildTypeId,
		BuildRobotCfgId:       tableGameSettingSheetGame.BuildRobotCfgId,
		InitialStrength:       tableGameSettingSheetGame.InitialStrength,
		Herouplvitems:         tableGameSettingSheetGame.Herouplvitems,
		Heroupstaritems:       tableGameSettingSheetGame.Heroupstaritems,
		Heroupskillitems:      tableGameSettingSheetGame.Heroupskillitems,
		Heroupequipitems:      tableGameSettingSheetGame.Heroupequipitems,
		Herocurrencyfragments: tableGameSettingSheetGame.Herocurrencyfragments,
		RobotStrength:         tableGameSettingSheetGame.RobotStrength,
		StrengthMax:           tableGameSettingSheetGame.StrengthMax,
	}
}

func GetTableItemSheetItemConfig(id int32) (configpb.StcTableItem_SheetItem_6508A6FE, bool) {
	config, ok := tableItemSheetItem[id]
	if !ok {
		zlog.Errorf("GetTableItemSheetItemConfig Err!!! id: %d", id)
		return configpb.StcTableItem_SheetItem_6508A6FE{}, false
	}
	return configpb.StcTableItem_SheetItem_6508A6FE{
		Id:         config.Id,
		Type:       config.Type,
		Bagshow:    config.Bagshow,
		Quality:    config.Quality,
		Accelerate: config.Accelerate,
		Par:        config.Par,
	}, true
}

func GetTableItemSheetItemMap() map[int32]*configpb.StcTableItem_SheetItem_6508A6FE {
	return tableItemSheetItem
}

func GetTableUnlockSheetUnlockByBuildLv() map[int32]map[int32]int32 {
	return systemUnlockMapByBuildLv
}

func GetTableUnlockSheetUnlockByHeroStar() map[int32][]int32 {
	return systemUnlockMapByHeroStar
}
